﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using ZFCNew.ZfcServiceReference;
using ZFCNew.Code;
using ZFCNew.Controls;
using System.Linq;

namespace ZFCNew.ViewModels
{
    public class TeamModel : INotifyPropertyChanged
    {
        public ICommand GetNextMatchCommand { get; private set; }
        public ICommand GetAllMatchesCommand { get; private set; }
        private readonly ServiceHelper sh = new ServiceHelper();
        private Team _selectedTeam { get; set; }
        public Team SelectedTeam
        {
            get { return _selectedTeam; }
            set { _selectedTeam = value; NotifyPropertyChanged("SelectedTeam"); }
        }
        public ObservableCollection<Team> Teams { get; private set; }

        public TeamModel()
        {
            Teams = new ObservableCollection<Team>();
            LoadTeams();
            GetNextMatchCommand = new DelegateCommand(GetNextMatch, (s) => true);
            GetAllMatchesCommand = new DelegateCommand(GetAllMatches, (s) => true);

            sh.GetTeamsCompleted += new EventHandler<DataEventArgs<ObservableCollection<Team>>>(sh_GetTeamsCompleted);
            sh.GetAllMatchesCompleted += new EventHandler<DataEventArgs<ObservableCollection<Schedule>>>(sh_GetAllMatchesCompleted);
            sh.GetNextMatchCompleted += new EventHandler<DataEventArgs<Schedule>>(sh_GetNextMatchCompleted);
        }

        void sh_GetNextMatchCompleted(object sender, DataEventArgs<Schedule> e)
        {
            var team1 = Teams.FirstOrDefault(t => string.Equals(t.Name.Trim(), e.Data.FirstTeam.Trim()));
            var team2 = Teams.FirstOrDefault(t => string.Equals(t.Name.Trim(), e.Data.SecondTeam.Trim()));
            var day = e.Data.Time.ToLocalTime().Date == DateTime.Now.Date ? "Today " : "Tomorrow ";

            if (team1 != null && team2 != null)
            {
                var data = new OneMatchData
                               {
                                   FirstTeam = e.Data.FirstTeam,
                                   SecondTeam = e.Data.SecondTeam,
                                   Time = e.Data.Time.ToLocalTime().ToShortTimeString(),
                                   Venu = e.Data.Venu,
                                   Id1 = team1.Id,
                                   Id2=team2.Id,
                                   TodayTommrow = day
                               };

                var som = new ShowOneMatch();
                som.DataContext = data;
                var win = new ZfcWindow();
                win.VerticalAlignment = VerticalAlignment.Top;
                win.Margin = new Thickness(0, 100, 0, 0);
                win.Content = som;
                win.Show();
            }
        }

        void sh_GetAllMatchesCompleted(object sender, DataEventArgs<ObservableCollection<Schedule>> e)
        {
            ObservableCollection<AllMatchData> data = new ObservableCollection<AllMatchData>();
            foreach (var item in e.Data)
            {
                data.Add
                    (
                    new AllMatchData
                        {
                            TeamOne = item.FirstTeam,
                            TeamTwo = item.SecondTeam,
                            Goal1 =item.FirstTeamGoal,
                            Goal2 = item.SecondTeamGoal,
                            Time = item.Time.ToLocalTime().ToString(),
                            Venu = item.Venu
                            
                        }
                    );
            }


            var am = new AllMatches();
            am.DataContext = data;



            var win = new ZfcWindow();
            win.VerticalAlignment = VerticalAlignment.Top;
            win.Margin = new Thickness(0, 70, 0, 0);
            win.Style = (Style) Application.Current.Resources["ZfcChildWindowFlatStyle"];

            win.Content = am;
            win.Show();

            // Notification
            MainPage.NotificationPop.SetMessage
                    (
                    new NotificationData
                    {
                        Message = "You can sort by clicking on any column header!"
                    }
                    );
            MainPage.NotificationPop.ShowWithDelay();
        }

        private void GetNextMatch(object s)
        {
            sh.GetNextMatch();
        }

        private void GetAllMatches(object s)
        {
            sh.GetAllMatches();
        }

        private void LoadTeams()
        {
            sh.GetTeams();
        }

        void sh_GetTeamsCompleted(object sender, DataEventArgs<ObservableCollection<Team>> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(
                () =>
                {
                    foreach (var t in e.Data)
                    {
                        Teams.Add(t);
                    }
                    SelectedTeam = e.Data[0];
                }
                );
        }
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }

    public class OneMatchData
    {
        public int Id1 { get; set; }
        public int Id2 { get; set; }
        public string FirstTeam { get; set; }
        public string SecondTeam { get; set; }
        public string Time { get; set; }
        public string Venu { get; set; }
        public string TodayTommrow { get; set; }
    }

    public class AllMatchData
    {
        public int? Goal1 { get; set; }
        public string TeamOne { get; set; }
        public string TeamTwo { get; set; }
        public int? Goal2 { get; set; }
        public string Time { get; set; }
        public string Venu { get; set; }
        
    }
}
